package approve.action;

import java.util.Calendar;
import java.util.Formatter;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;

public class DateTimePicker extends RelativeLayout {
	TextView display;

	public interface OnDateTimeChangedListener {
		/**
		 * @param view
		 *            The view associated with this listener.
		 * @param year
		 *            The year that was set.
		 * @param monthOfYear
		 *            The month that was set (0-11) for compatibility with
		 *            {@link java.util.Calendar}.
		 * @param dayOfMonth
		 *            The day of the month that was set.
		 * @param hourOfDay
		 *            The current hour.
		 * @param minute
		 *            The current minute.
		 */
		void onDateTimeChanged(DateTimePicker view, int year, int monthOfYear,
				int dayOfMonth, int hourOfDay, int minute);
	}

	private OnDateTimeChangedListener onDateTimeChangedListener;

	private final DatePicker datePicker;
	private final TimePicker timePicker;

	/**
	 * @param context
	 */
	public DateTimePicker(final Context context) {
		this(context, null);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public DateTimePicker(final Context context, final AttributeSet attrs) {
		super(context, attrs);

		// Layout
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.datetime_picker, this, true);
		display = (TextView) findViewById(R.id.display);
		// Attribute
		Calendar calendar = Calendar.getInstance();
		final int _currentYear = calendar.get(Calendar.YEAR);
		final int _currentMonth = calendar.get(Calendar.MONTH);
		final int _currentDay = calendar.get(Calendar.DAY_OF_MONTH);
		final int _currentHour =calendar.get(Calendar.HOUR_OF_DAY);
		final int _currentMinute = calendar.get(Calendar.MINUTE);

		// DatePicker
		datePicker = (DatePicker) findViewById(R.id._babukuma_datetime_picker_date_picker);
		datePicker.init(_currentYear, _currentMonth, _currentDay,
				new OnDateChangedListener() {

					@Override
					public void onDateChanged(final DatePicker view,
							final int year, final int monthOfYear,
							final int dayOfMonth) {
						if (onDateTimeChangedListener != null) {
							onDateTimeChangedListener.onDateTimeChanged(
									DateTimePicker.this, year, monthOfYear,
									dayOfMonth, timePicker.getCurrentHour(),
									timePicker.getCurrentMinute());
						}
						updateDisplayMessage();
					}

				});

		// TimePicker
		timePicker = (TimePicker) findViewById(R.id._babukuma_datetime_picker_time_picker);
		timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
			@Override
			public void onTimeChanged(final TimePicker view,
					final int hourOfDay, final int minute) {
				if (onDateTimeChangedListener != null) {
					onDateTimeChangedListener.onDateTimeChanged(
							DateTimePicker.this, datePicker.getYear(),
							datePicker.getMonth(), datePicker.getDayOfMonth(),
							hourOfDay, minute);
				}
				updateDisplayMessage();
			}
		});
		timePicker.setCurrentHour(_currentHour);
		timePicker.setCurrentMinute(_currentMinute);
		updateDisplayMessage();
	}

	private void updateDisplayMessage() {
		display.setText(getMonth() + "-"+getDayOfMonth() + "-" + getYear()+" "+getCurrentHour()+":"+getCurrentMinute()+":00");
	}

	public void setOnDateTimeChangedListener(
			OnDateTimeChangedListener onDateTimeChangedListener) {
		this.onDateTimeChangedListener = onDateTimeChangedListener;
	}

	public void updateDateTime(int year, int monthOfYear, int dayOfMonth,
			int currentHour, int currentMinute) {
		datePicker.updateDate(year, monthOfYear, dayOfMonth);
		timePicker.setCurrentHour(currentHour);
		timePicker.setCurrentMinute(currentMinute);
	}

	public void updateDate(int year, int monthOfYear, int dayOfMonth) {
		datePicker.updateDate(year, monthOfYear, dayOfMonth);
	}

	public void setIs24HourView(final boolean is24HourView) {
		timePicker.setIs24HourView(is24HourView);
	}

	public String getYear() {
		Formatter fm=new Formatter();
		fm.format("%02d", datePicker.getYear());
		return fm.toString();
	}

	public String getMonth() {
		Formatter fm=new Formatter();
		fm.format("%02d", datePicker.getMonth()+1);
		return fm.toString();
	}

	public String getDayOfMonth() {
		Formatter fm=new Formatter();
		fm.format("%02d", datePicker.getDayOfMonth());
		return fm.toString();
	}

	public DatePicker getDatePicker() {
		return datePicker;
	}

	public String getCurrentHour() {
		Formatter fm=new Formatter();
		fm.format("%02d", timePicker.getCurrentHour());
		return fm.toString();
	}

	public String getCurrentMinute() {
		Formatter fm=new Formatter();
		fm.format("%02d", timePicker.getCurrentMinute());
		return fm.toString();
	}


}
